//
//  CustomSearchView.m
//  TengyunStation
//
//  Created by Johnny on 15/9/13.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "CustomSearchView.h"
#import "TSHeader.h"

#define TextFieldWidth          304.0
#define TextFieldSmalWidth      270.0
#define TextFieldHeight         29.0

#define CancelBtnWidth          40

@implementation CustomSearchView

- (void)awakeFromNib {
    self.backgroundColor = UIColorFromRGB(0xeaeaea);
    self.layer.borderColor = LineColor.CGColor;
    self.layer.borderWidth = 0.5;
    self.searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(8, (self.frame.size.height - TextFieldHeight)/2.0, TextFieldWidth, TextFieldHeight)];
    self.searchTextField.backgroundColor = [UIColor whiteColor];
    self.searchTextField.clipsToBounds = YES;
    self.searchTextField.layer.cornerRadius = 4;
    self.searchTextField.font = [UIFont systemFontOfSize:14];
    self.searchTextField.textColor = MainTextColor;
    //    self.searchTextField.enablesReturnKeyAutomatically = YES;
    self.searchTextField.returnKeyType = UIReturnKeySearch;
    self.searchTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    self.searchTextField.placeholder = @"收件人手机号/运单号/网点名称";
    self.searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.searchTextField.delegate = self;
    UIButton *sarchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sarchBtn.frame = CGRectMake(0, 0, 30, 30);
    [sarchBtn setImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
    self.searchTextField.leftView = sarchBtn;
    self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
    UIButton *scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    scanBtn.frame = CGRectMake(0, 0, 40, 40);
    [scanBtn setImage:[UIImage imageNamed:@"search_icon_erw"] forState:UIControlStateNormal];
    [scanBtn setImage:[UIImage imageNamed:@"search_icon_erw_pre"] forState:UIControlStateHighlighted];
    [scanBtn addTarget:self action:@selector(startScanningAction) forControlEvents:UIControlEventTouchUpInside];
    self.searchTextField.rightView = scanBtn;
    self.searchTextField.rightViewMode = UITextFieldViewModeUnlessEditing;
    [self.searchTextField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:self.searchTextField];
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBtn.frame = CGRectMake((self.frame.size.width - CancelBtnWidth), 0, CancelBtnWidth, self.frame.size.height);
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:OrangeColor forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_cancelBtn addTarget:self action:@selector(cancelSearchAction) forControlEvents:UIControlEventTouchUpInside];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (void)startTextFieldEditing {
    [_searchTextField becomeFirstResponder];
}

- (void)startScanningAction {
    [self.searchTextField resignFirstResponder];
    if ([_delegate respondsToSelector:@selector(startScanning)]) {
        [_delegate startScanning];
    }
}

- (void)cancelSearchAction {
    [self.searchTextField resignFirstResponder];
    if ([_delegate respondsToSelector:@selector(cancelSearch)]) {
        [_delegate cancelSearch];
    }
}

- (void)textFieldEditingChanged:(UITextField *)textField {
    if ([[UITextInputMode currentInputMode].primaryLanguage isEqualToString:@"zh-Hans"]) {
        UITextRange *textRange = [textField markedTextRange];
        UITextPosition *textPosition = [textField positionFromPosition:textRange.start offset:0];
        if (!textPosition) {
            [self caculateTextField:textField.text];
        }
    } else {
        [self caculateTextField:textField.text];
    }
}

- (void)caculateTextField:(NSString *)text {
    if ([_delegate respondsToSelector:@selector(textEditingChanged:)]) {
        [_delegate textEditingChanged:text];
    }
}

- (void)textFieldBecomeFirstResponder {
    _isShowCancel = YES;
    self.searchTextField.frame = CGRectMake(self.searchTextField.frame.origin.x, self.searchTextField.frame.origin.y, TextFieldSmalWidth, self.searchTextField.frame.size.height);
    [self addSubview:_cancelBtn];
}

- (void)textFieldResignFirstResponder {
    _isShowCancel = NO;
    self.searchTextField.frame = CGRectMake(self.searchTextField.frame.origin.x, self.searchTextField.frame.origin.y, TextFieldWidth, self.searchTextField.frame.size.height);
    [_cancelBtn removeFromSuperview];
}

- (void)setIsShowCancel:(BOOL)isShowCancel {
    if (_isShowCancel != isShowCancel) {
        _isShowCancel = isShowCancel;
        if (isShowCancel) {
            [self textFieldBecomeFirstResponder];
        } else {
            [self textFieldResignFirstResponder];
        }
    }
}

- (void)setIsShowScanning:(BOOL)isShowScanning {
    _isShowScanning = isShowScanning;
    if (isShowScanning) {
        self.searchTextField.rightViewMode = UITextFieldViewModeUnlessEditing;
    } else {
        self.searchTextField.rightViewMode = UITextFieldViewModeNever;
    }
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!self.isShowCancel) {
        [self textFieldBecomeFirstResponder];
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([_delegate respondsToSelector:@selector(customSearchLimitText:)]) {
        return [_delegate customSearchLimitText:string];
    } else {
        return YES;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if ([_delegate respondsToSelector:@selector(startSearchText:)]) {
        [_delegate startSearchText:textField.text];
    }
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}
@end
